	/*
 * main.C
 *
 *  Created on: 1 déc. 2010
 *      Author: mbenoit
 */
#include "CTDR_Interaction.h"
#include "CTDR_ConstantField.h"
#include "CTDR_2DField.h"
#include "CTDR_Animation.h"
#include "CTDR_Electrode.h"
#include "CTDR_Strip.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "TGraph.h"
#include "CTDR_Global.h"

#include "global.h"


vector<double> GenerateVector(double x0,double xf,int N){

	vector<double> a_vector;
	for (int i=0;i<N;i++){
		a_vector.push_back(gCTDR.rand->Uniform(x0,xf));
	}
	return a_vector;
}



int main(int argc, char *argv[])
{

	// Generate electric field object, derived from CTDR_Field
	//CTDR_ConstantField F(1000.,1000.,1000.,10000.);
	//CTDR_2DField F(Lx,Ly,zmax,"/Users/Mathieu/Documents/workspace/MC_LAL_CTDR/Ex.dat","/Users/Mathieu/Documents/workspace/MC_LAL_CTDR/Ey.dat","ShiftedGR1_depth=300_edge=100_bias=-500_eField.root");
	CTDR_2DField F(1592.5,240,zmax,"E_5e14_shift=100.root");
	//CTDR_2DField F(1595.5,80,zmax,"E_shifted100.root");


	// Generate the Ramo Potential Signal generator
	//CTDR_Strip *strip = new CTDR_Strip(1595.5,1595.5,zmax,"V_shifted100.root");
	CTDR_Strip *strip = new CTDR_Strip(1592.5,1592.5,zmax,"V_shift=100.root");

	//int NEvent=25;
	//double dX=550.0/(NEvent-1);



	cout << TString::Format("[Main] Beginning event loop") << endl;

	//Event loop

	int npx=10;
	int npy=2;

	double dX=400.0/(npx-1);
	double dY=300.0/(npy-1);
	double xhit=1192.50;
	double yhit=0;


	TGraph *gr = new TGraph(npx);
	gr->SetName("CCE2D");
	int npoints = 0;
	for(int i=0;i<npx;i++){


		for(int j=0;j<npy-1;j++){

			CTDR_Interaction e(GenerateVector(xhit+i*dX,xhit+i*dX,2000),GenerateVector(yhit,yhit,2000),GenerateVector(zmax,0,2000),3.4*22000,0.001,0,-1);
			CTDR_Interaction h(GenerateVector(xhit+i*dX,xhit+i*dX,2000),GenerateVector(yhit,yhit,2000),GenerateVector(zmax,0,2000),3.4*22000,0.001,0,1);

			//CTDR_Interaction h(xhit+i*dX,0,yhit+j*dY,3.4*22000,0.001,0,1000, 1);
			CTDR_Animation animation(Lx,Ly,zmax,e,h,0,npoints);
			int it=0;

			while((h.Gett()<tmax) | (e.Gett()<tmax))
			{

					if(h.Gett()<tmax && e.Gett()<tmax){
						cout << "[CTDR_Interaction] Electrons";
						e.iteration(F);
						cout << "Holes";
						h.iteration(F);


					}

					else if(e.Gett()<tmax){
						cout << "[CTDR_Interaction] Electrons";
						e.iteration(F);

					}
					else if(h.Gett()<tmax){
						cout << "[CTDR_Interaction] Holes";
						h.iteration(F);

					}


					cout << endl;
					if(it%100==0)animation.AjoutePas(e,h);
					strip->AddData(e,h);

				it++;
			};
			animation.Write();
			strip->WriteData(npoints,e,h);
			cout << "Qtot=" << strip->GetQtot() << endl;
			gr->SetPoint(npoints,xhit+i*dX,strip->GetQtot());
			npoints++;
			strip->Reset();

		};
	};

	TFile *fichier= new TFile("2DCCE_3.root","recreate");
	gr->Write();
	fichier->Close();






	/*for (int event=0;event<NEvent;event++){

		cout << TString::Format("[Main] Generate track parameters for event %d",event) << endl;
		double track[6];
		double xtrack=gCTDR.rand->Uniform(0,Lx);
		double ytrack=gCTDR.rand->Uniform(0,Ly);
//		track[0]=gCTDR.rand->Uniform(0,Lx);
//		track[1]=gCTDR.rand->Uniform(0,Lx);
//		track[2]=gCTDR.rand->Uniform(0,Ly);
//		track[3]=gCTDR.rand->Uniform(0,Ly);
//		track[4]=gCTDR.rand->Uniform(0,zmax);
//		track[5]=gCTDR.rand->Uniform(0,zmax);
		track[0]=1000+event*dX;
		track[1]=1000+event*dX;
		track[2]=0;
		track[3]=0;
		track[4]=0;
		track[5]=zmax;


	// Generate the interaction, a group of carriers associated to a G4Hit

	CTDR_Interaction e(track[0],0,3.4*22000,0.1,0,1000,-1);
	CTDR_Interaction h(track[0],0,3.4*22000,0.1,0,1000, 1);
	//CTDR_Interaction e(xtr,ytr,ztr,22000,1,0,-1);
	//CTDR_Interaction h(xtr,ytr,ztr,22000,1,0,1);

		//CTDR_Interaction e(track,3.6*11000,200,-1);
		//CTDR_Interaction h(track,3.6*11000,200, 1);


	//Animation object used to visualize the event and access the data from individual tracks
		CTDR_Animation animation(Lx,Ly,zmax,e,h,0,event);

	int i=0;


	//Initialize Ramo computation
	strip->SetV0(e,h);


	//Simulation loop, limited to 20ns

	while((h.Gett()<tmax) | (e.Gett()<tmax))
	{

			if(h.Gett()<tmax && e.Gett()<tmax){
				cout << "[CTDR_Interaction] Electrons";
				e.iteration(F);
				cout << "Holes";
				h.iteration(F);


			}

			else if(e.Gett()<tmax){
				cout << "[CTDR_Interaction] Electrons";
				e.iteration(F);

			}
			else if(h.Gett()<tmax){
				cout << "[CTDR_Interaction] Holes";
				h.iteration(F);

			}


			cout << endl;
			if(i%100==0)animation.AjoutePas(e,h);
			strip->AddData(e,h);

		i++;
	};


	animation.Write();
	strip->WriteData(event,e,h);
	strip->Reset();


	};*/

	return 0;

}
